#include <cstdio>
#include <map>
#include <queue>
using namespace std;

const int maxt = 1005;

int main() {
  int t, kase = 0;
  while ((scanf("%d", &t) == 1) && t) {
    printf("Scenario #%d\n", ++kase);

    int i;
    map<int, int> team;
    for (i = 0; i < t; i++) {
      int n, p;
      scanf("%d", &n);
      while (n--) {
        scanf("%d", &p);
        team[p] = i;
      }
    }

    queue<int> tq, q[maxt];
    char cmd[10];
    int p;
    while (true) {
      scanf("%s", cmd);
      if (cmd[0] == 'S') {
        break;
      }
      else {
        scanf("%d", &p);
        if (cmd[0] == 'E')  {
          int p_team = team[p];
          if (q[p_team].empty()) {
            tq.push(p_team);
          }
          q[p_team].push(p);
        }
        else if (cmd[0] == 'D') {
          int front_tq = tq.front();
          printf("%d\n", q[front_tq].front());
          q[front_tq].pop();
          if (q[front_tq].empty()) {
            tq.pop();
          }
        }
      }
    }

    printf("\n");
  }

  return 0;
}
